package com.gurun.camera.addDevice.presenter;

import android.content.Context;

import com.danale.sdk.platform.constant.device.OnlineType;
import com.danale.sdk.platform.entity.deviceinfo.DeviceAddedOnlineInfoV4;
import com.danale.sdk.utils.LogUtil;
import com.gurun.camera.addDevice.model.CheckDevStatusModelImpl;
import com.gurun.camera.addDevice.model.ICheckDevStatusModel;
import com.gurun.camera.addDevice.view.ICheckDevOnlineView;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Check the device on-line status
 * Created by Administrator on 2016/9/19.
 */
public class CheckDevOnlinePresenterImpl implements ICheckDevOnlinePresenter {
    private static final String TAG = CheckDevOnlinePresenterImpl.class.getSimpleName();
    private final String mDeviceId;
    private ICheckDevOnlineView mView;
    private ICheckDevStatusModel mCheckStatusModel;
    private Context mContext;
    private Subscription mCheckSubscrip;
    private Subscription mProcessSubscrip;

    public CheckDevOnlinePresenterImpl(Context context, ICheckDevOnlineView view, String deviceId){
        mContext = context;
        mView = view;
        mDeviceId = deviceId;
        mCheckStatusModel = new CheckDevStatusModelImpl();
    }

    @Override
    public void startProgress() {
        mProcessSubscrip = Observable.range(0, 101)
                .concatMap(new Func1<Integer, Observable<Integer>>() {
                    @Override
                    public Observable<Integer> call(Integer integer) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return Observable.just(integer);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        if(integer == 100){
                            stopCheckOnlineStatus();
                            if (mView != null) {
                                mView.onDevOnlineFailed(mDeviceId);
                            }
                        }else{
                            if (mView != null) {
                                mView.onDevOnlineProgress(integer);
                            }
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                    }
                }, new Action0() {
                    @Override
                    public void call() {
                        LogUtil.d(TAG, "stop progress");
                    }
                });
    }

    @Override
    public void stopProgress() {
        if(mProcessSubscrip != null && !mProcessSubscrip.isUnsubscribed()){
            mProcessSubscrip.unsubscribe();
        }
    }

    /**
     * Check the device online status every six seconds
     * @param deviceId
     */
    @Override
    public void startCheckOnlineStatus(final String deviceId) {
        mCheckSubscrip = Observable.interval(2000, 6000, TimeUnit.MILLISECONDS)
                .flatMap(new Func1<Long, Observable<DeviceAddedOnlineInfoV4>>() {
                    @Override
                    public Observable<DeviceAddedOnlineInfoV4> call(Long aLong) {
                        return mCheckStatusModel.checkDeviceState(mDeviceId);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<DeviceAddedOnlineInfoV4>() {
                    @Override
                    public void call(DeviceAddedOnlineInfoV4 deviceAddedOnlineInfoV4) {
                        if(deviceAddedOnlineInfoV4.getOnlineType() == OnlineType.ONLINE){
                            if(mView != null){
                                mView.onDevIsOnline(mDeviceId);
                            }
                            stopCheckOnlineStatus();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                    }
                });
    }

    @Override
    public void stopCheckOnlineStatus() {
        if(mCheckSubscrip != null && !mCheckSubscrip.isUnsubscribed()){
            mCheckSubscrip.unsubscribe();
        }
    }

}
